package zhaolongli_Controller;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import zhaolongli_Boundary.DrawingCanvas;
import zhaolongli_Boundary.EdgeWarning;
import zhaolongli_Entity.Edge;
import zhaolongli_Entity.Model;

public class EdgeController extends MouseAdapter {

	// location of mouse click
	Point start = null;
	// reference of model
	Model m;
	// Canvas to draw edges
	DrawingCanvas view;

	// Controller constructor
	
	public EdgeController(Model m, DrawingCanvas view){
		this.m = m;
		this.view = view;
	}
	
	public void mousePressed(MouseEvent me) {
		if(m.isReady() == false){
			new EdgeWarning();
			return;
		}
		
		Edge edge;
		int column;
		boolean added;
		start = me.getPoint();
		// System.out.println("x: "+ start.x + "   y: "+ start.y);
		if (start.y > 60 && start.y < 340 && start.x > m.getOffset() && start.x < (m.getGap() * m.getN())){
			column = (start.x - m.getOffset())/m.getGap();
			edge = new Edge(column, start.y);
			added = m.addEdge(edge);
			if(added == true){
				view.repaint();
			}			
		}
	}
	
}
